package main

import (
	"fmt"
	"math/rand"
	"sort"
)

type Hero struct {
	Name string
	Age  int
}

type HeroSlice []Hero

// 实现sort接口 Interface
func (heroSlice HeroSlice) Len() int {
	return len(heroSlice)
}

// 排序的标准 年龄从小到大
func (heroSlice HeroSlice) Less(i, j int) bool {
	return heroSlice[i].Age < heroSlice[j].Age
	// 按name排序
	//return heroSlice[i].Name < heroSlice[j].Name
}
func (heroSlice HeroSlice) Swap(i, j int) {
	//temp := heroSlice[i]
	//heroSlice[i] = heroSlice[j]
	//heroSlice[j] = temp
	heroSlice[i], heroSlice[j] = heroSlice[j], heroSlice[i]
}

func main() {
	var intSlice = []int{0, 14, -1, 2, 5}
	sort.Ints(intSlice)
	fmt.Println(intSlice)

	var heros HeroSlice
	for i := 0; i < 10; i++ {
		hero := Hero{
			Name: fmt.Sprintf("英雄:%d", rand.Intn(100)),
			Age:  rand.Intn(100),
		}
		heros = append(heros, hero)
	}
	for _, v := range heros {
		fmt.Println(v)
	}
	fmt.Println("==排序==")
	sort.Sort(heros)
	for _, v := range heros {
		fmt.Println(v)
	}
}
